from rest_framework import serializers
from .models import *


# 品牌的序列化器
class BrandSer(serializers.ModelSerializer):

    id = serializers.IntegerField(read_only=True)
    logo = serializers.CharField(allow_null=True)

    class Meta:
        model = Brand
        fields = '__all__'


# 频道组的序列化器
class GroupSer(serializers.ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.all())
                                                    # 只做序列化输出
    cate_name = serializers.SerializerMethodField(read_only=True)

    def get_cate_name(self,obj):
        return obj.cate.name

    class Meta:
        model = Group
        fields = '__all__'
        read_only_fields = ["id",]


# 商品类别的序列化器
class CateSer(serializers.ModelSerializer):

    class Meta:
        model = Cate
        fields = "__all__"


# SKU的序列化器

class SKUSer(serializers.ModelSerializer):
                                                # 把函数的查询集赋给queryset
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.all())
    cate_name = serializers.SerializerMethodField(read_only=True)

    def get_cate_name(self, obj):
        return obj.cate.name

    class Meta:
        model = SKU
        fields = "__all__"
        read_only_fields = ["id", "detail", "image_default", "sales",]


# SPU的序列化器
class SPUSer(serializers.ModelSerializer):
    brand = serializers.PrimaryKeyRelatedField(queryset=Brand.objects.all())
    brand_name = serializers.SerializerMethodField(read_only=True)

    def get_brand_name(self,obj):
        return obj.brand.name

    class Meta:
        model = SPU
        fields = "__all__"
        read_only_fields = ["id",]


# SPU规格管理序列化器
class SPUSpecificationSer(serializers.ModelSerializer):

    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())
    spu_name = serializers.SerializerMethodField(read_only=True)

    def get_spu_name(self,obj):
        return obj.spu.name

    class Meta:
        model = SPUSpecification
        fields = "__all__"
        read_only_fields = ["id",]


# 规格选项值的序列化器
class SpecificationOptionSer(serializers.ModelSerializer):
    spec = serializers.PrimaryKeyRelatedField(queryset=SPUSpecification.objects.all())
    spec_name = serializers.SerializerMethodField(read_only=True)

    def get_spec_name(self,obj):
        return obj.spec.name


    class Meta:
        model = SpecificationOption
        fields = "__all__"
        ewad_only_fields = ["id",]


class SKUImageSer(serializers.ModelSerializer):

    sku = serializers.PrimaryKeyRelatedField(queryset=SKU.objects.all())
    sku_name = serializers.SerializerMethodField(read_only=True)
    def get_sku_name(self, obj):
        return obj.sku.name

    class Meta:
        model = SKUImage
        fields = "__all__"
        read_only_fields = ["id",]